package com.azt.provider.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.azt.api.pojo.Brand;
import com.azt.api.pojo.OldProduct;
import com.azt.api.service.ProBrandService;
import com.azt.model.AjaxJson;
import com.azt.model.page.Pagination;
import com.azt.provider.mapper.BrandMapper;
import com.azt.provider.mapper.OldProductMapper;
import com.azt.utils.BeanUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import tk.mybatis.mapper.entity.Example;

@Service
@Transactional
public class ProBrandServiceImpl implements ProBrandService {

	@Autowired
	private BrandMapper brandMapper;

	@Autowired
	private OldProductMapper productMapper;

	@Override
	public Pagination getProBrandPage(int pageno, int pagesize, Map<String, String> searchMap) {
		String state = searchMap.get("so_state");
		String name = searchMap.get("so_name");
		Map<String, String> searchNewMap = new HashMap<String, String>();
		if (StringUtils.isNotBlank(state)) {
			searchNewMap.put("state", state);
		}
		if (StringUtils.isNotBlank(name)) {
			searchNewMap.put("name", name);
		}
		

		Page page = PageHelper.startPage(pageno, pagesize);
		List<Brand> brands = brandMapper.getProBrandPage(searchNewMap);

		Pagination pagination = new Pagination(page, brands);
		pagination.setList(brands);
		pagination.setParams(searchMap);
		return pagination;
	}

	/**
	 * 删除品牌，只有未关联商品的才能被删除，逻辑删除
	 */
	@Override
	public AjaxJson removeBrand(int id) {
		try {
			OldProduct product = new OldProduct();
			product.setDelflag(0);
			product.setBrandid(id);
			List<OldProduct> proList = productMapper.select(product);

			if (proList != null && proList.size() > 0) {
				return AjaxJson.error("该品牌下面已有产品,不允许删除");
			} else {
				Brand brand = new Brand();
				brand.setId(id);
				brand.setRemoved(1);
				brandMapper.updateByPrimaryKeySelective(brand);
				return AjaxJson.success();
			}
		} catch (Exception e) {
			return AjaxJson.error("操作失败");
		}
	}

	/**
	 * 审核品牌
	 */
	@Override
	public void auditBrand(int id, int state) {
		Brand brand = new Brand();
		brand.setId(id);
		brand.setState(state);
		brandMapper.updateByPrimaryKeySelective(brand);
	}

	@Override
	public Brand getBrandById(int id) {
		Brand brand = new Brand();
		brand.setId(id);
		brand.setRemoved(0);
		return brandMapper.selectOne(brand);
	}

	@Override
	public void saveBrand(Brand brand) {
		if (brand != null) {
			if (brand.getId() == null) {
				brandMapper.insert(brand);
			} else {
				brandMapper.updateByPrimaryKey(brand);
			}
		}
	}

	@Override
	public List<Map<String, Object>> getLikeName(String queryName, Integer pageNo) {
		Example example = new Example(Brand.class);
		example.createCriteria().andLike("name", "%"+queryName+"%");
		//PageHelper.startPage(0, pageNo);
		List<Brand> brandlist = brandMapper.selectByExample(example);

		List<Map<String, Object>> list = new ArrayList<>();
		for (Brand brand : brandlist) {
			Map m = BeanUtils.copyBean2Map(brand);
			list.add(m);
		}
		
		return list;
	}

}
